package com.flymobi.admin;

import java.util.List;

import com.flymobi.fw.modules.SM;
import com.flymobi.po.Manager;
import com.flymobi.po.Roles;
import com.flymobi.util.C;
import com.flymobi.util.Pagination;
import com.flymobi.util.WebContext;

/**
 * 
 * 
 * Title:
 * 
 * Description:
 * 
 * Copyright: Copyright (c) 2008
 * 
 * @author haoxz11
 * @created Oct 21, 2008 11:16:42 AM
 * @version $Id: ManagerAction.java,v 1.1 2010/04/09 03:27:08 airsou Exp $
 */
public class ManagerAction extends WebAction {
	private Manager manager;

	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String webProcess(WebContext context, Manager m) {
		if ("add".equals(method)) {
			List<Roles> rolesList=SM.RolesService.list();
			context.put("rolesList", rolesList);
			return "edit";
		} else if ("editme".equals(method)) {
			context.put("man", m);
			return "editme";
		}else if ("edit".equals(method)) {
			Manager man = SM.ManagerService.get(
					id);
			List<Roles> rolesList=SM.RolesService.list();
			context.put("man", man);
			context.put("rolesList", rolesList);
			return "edit";
		} else if ("delete".equals(method)) {
			SM.ManagerService.delete(id);
			context.put("rmsg", C.TEXT.DELETE_SUCCESS);
			method = "list";
			return this.webProcess(context, m);
		} else if ("save".equals(method)) {
			Manager mTemp;
			if(this.manager.getId()==null){
				mTemp=this.manager;
				mTemp.setStatus(1L);
			}else{
				mTemp=SM.ManagerService.get(manager.getId().toString());
				mTemp.setName(manager.getName());
				mTemp.setPwd(manager.getPwd());
				mTemp.setRolesId(manager.getRolesId());
				mTemp.setStatus(manager.getStatus());
			}
			SM.ManagerService.save(mTemp);
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			method = "list";
			return this.webProcess(context, m);
		}else if ("start".equals(method)) {
			Manager a=SM.ManagerService.get(id);
			a.setStatus(1L);
			SM.ManagerService.save(a);
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			//return "save";
			method = "list";
			return this.webProcess(context, m);
		}else if ("stop".equals(method)) {
			Manager a=SM.ManagerService.get(id);
			a.setStatus(0L);
			SM.ManagerService.save(a);
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			//return "save";
			method = "list";
			return this.webProcess(context, m);
		}else if ("saveme".equals(method)) {
			m.setPwd(manager.getPwd());
			SM.ManagerService.save(m);
			context.put("rmsg", C.TEXT.SAVE_SUCCESS);
			method = "editme";
			return this.webProcess(context, m);
		} else if ("list".equals(method)) {
			List<Manager> managerList = SM.ManagerService.list(key,startRow,
					pagePerSize);
			context.put("managerList", managerList);
			context.put("pagenation", new Pagination(SM.ManagerService
					.getCount(key).intValue(), startRow, pagePerSize));
			return "list";
		} else {
			method = "list";
			return this.webProcess(context, m);
		}
	}
}
